﻿using UnityEngine;

namespace XLua_Ki
{
    public class BundleRequest : MonoBehaviour
    {

        //清除加载过的AB.
        public static void ClearBundles(string mainABName)
        {
            AssetBundle mainAB = null;
            if (GameManager_Ki.Instance.GetResourcesManager_Ki().MainBundels.TryGetValue(mainABName, out mainAB))
            {
                var manifest = mainAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
                var bundlesNames = manifest.GetAllAssetBundles();
                foreach (var item in bundlesNames)
                {
                    var bundles = GameManager_Ki.Instance.GetResourcesManager_Ki().Bundles_AB_GAME;
                    foreach (var item1 in bundles[item].GetAllAssetNames())
                    {
                        var prefabName = Util.GetFileNameWithoutExtension(item1);
                        if (GameManager_Ki.Instance.GetResourcesManager_Ki().Prefabs_AB_GAME.ContainsKey(prefabName))
                        {
                            GameManager_Ki.Instance.GetResourcesManager_Ki().Prefabs_AB_GAME.Remove(prefabName);
                        }
                    }
                    bundles[item].Unload(true);
                    bundles.Remove(item);
                }
                mainAB.Unload(true);
            }
        }


        //根据prefab获取对应的Bundle
        public static string MatchBundleName(string prefabName)
        {
            object bundleName = LuaUtil.CallLuaPathFunc(GameManager_Ki.Instance.LuaEnv, "CtrlManager.GetBundle", UnityEngine.SceneManagement.SceneManager.GetActiveScene().name, prefabName);
            return bundleName.ToString();
        }
    }
}
